import { t } from 'i18next'
import jumpPage from '@/utils/jumpPage'
import { Tab } from '@kedacom/react-next'
import { StyledWrap } from './style'
import { useGlobalStore } from '@/store/global'
import { checkPermission } from '@/utils'

export default function ScheduleTab({ tabIndex }: { tabIndex: number }) {
	const SWITCH = useGlobalStore((state) => state.SWITCH)
	/*
	let data = [
		true && { label: '列表', value: 1 },
		false && { label: '周', value: 2 },
		true && { label: '历史会议', value: 3 },
	]

	打印结果：
	[
		{
			"label": "列表",
			"value": 1
		},
    	false,
		{
			"label": "历史会议",
			"value": 3
		}
	]

	--------------------

	let data = [
		true && { label: '列表', value: 1 },
		false && { label: '周', value: 2 },
		true && { label: '历史会议', value: 3 },
	].filter(Boolean)


	打印结果：
	[
		{
			"label": "列表",
			"value": 1
		},
		{
			"label": "历史会议",
			"value": 3
		}
	]


	---------------------

	# Array.filter(Boolean)

	```
	var a=[1,2,"b",0,{},"",NaN,3,undefined,null,5];
	var b=a.filter(Boolean); // [1,2,"b",{},3,5]
	```

	Boolean 是一个函数，它会对遍历数组中的元素，并根据元素的真假类型，对应返回 true 或 false.

	例如：
	```
	Boolean(0); // false
	Boolean(true); // true
	Boolean(1); // true
	Boolean(""); // false
	Boolean("false"); // true. "false"是一个非空字符串
	```
	实际上，下面这样的写法是一种简写模式

	```
	b = a.filter(Boolean);
	```

	它等价于
	```
	b = a.filter(function (x) { return Boolean(x); });
	```

	*/
	let data = [
		checkPermission('100', SWITCH.meetingListTab) && { label: t('列表'), value: 1 },
		checkPermission('010', SWITCH.meetingListTab) && { label: t('周'), value: 2 },
		checkPermission('001', SWITCH.meetingListTab) && { label: t('历史会议'), value: 3 },
	].filter(Boolean) as { label: string; value: number }[]

	const handleChange = (val: number) => {
		switch (val) {
			case 1:
				jumpPage('meetingList')
				break
			case 2:
				jumpPage('myMeeting')
				break
			case 3:
				jumpPage('historyMeeting')
				break
		}
	}
	return (
		<>
			<StyledWrap className="meeting-schedule-tab">
				<Tab type="button" data={data} value={tabIndex} onChange={(e, v) => handleChange(v as number)} />
			</StyledWrap>
		</>
	)
}
